package YZ.Students.Mier.Dao.impl;


import YZ.Students.Mier.Dao.CommentDao;
import YZ.Students.Mier.Tool.utils.druidTool;
import YZ.Students.Mier.entity.CommentEntity;
import YZ.Students.Mier.entity.UserEntity;
import YZ.Students.Mier.vo.CommentVo;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;

import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class CommentDaoImpl implements CommentDao {

    private QueryRunner queryRunner = new QueryRunner(druidTool.dataSource);

    @Override
    public Integer insert(CommentEntity com) throws Exception {
        String sql = "insert into t_comment(userId,entityType,entityId,targeId,content,status,createTime) values(?,?,?,?,?,?,?)";
        return queryRunner.update(sql, com.getUserId(), com.getEntityType(), com.getEntityId(), com
                .getTargetId(), com.getContent(), com.getStatus(), com.getCreateTime());
    }

    @Override
    public CommentEntity selectById(Integer id) throws Exception {
        return null;
    }

    @Override
    public Integer updateById(CommentEntity commentEntity) throws Exception {
        return null;
    }

    @Override
    public Integer deleteById(Integer id) throws Exception {
        return null;
    }

    @Override
    public List<CommentEntity> selectList() throws Exception {
        return null;
    }

    @Override
    public Integer batchRemove(List<Integer> ids) throws Exception {
        return null;
    }

    @Override
    public List<CommentVo> commentListByInvitataionId(Integer id) throws Exception {
        String sql = "SELECT c.targeId, c.content, c.createTime, u.username, u.headerUrl,u.id as uid FROM t_comment c LEFT JOIN t_user u ON (u.id = c.userId) WHERE c.entityType = 1 AND c.entityId = ?";
        return queryRunner.query(sql, new ResultSetHandler<List<CommentVo>>() {
            @Override
            public List<CommentVo> handle(ResultSet resultSet) throws SQLException {
                // 1.准备集合
                List<CommentVo> commentVoList = new ArrayList<>();

                // 2.从结果集中获取数据
                while (resultSet.next()) {
                    int targetId = resultSet.getInt("targeId");
                    String content = resultSet.getString("content");
                    Date createTime = resultSet.getDate("createTime");
                    String username = resultSet.getString("username");
                    String headerUrl = resultSet.getString("headerUrl");
                    int uid = resultSet.getInt("uid");

                    // 封装评论人的信息
                    UserEntity userEntity = new UserEntity();
                    userEntity.setHeaderUrl(headerUrl);
                    userEntity.setUsername(username);
                    userEntity.setId(uid);

                    // 封装的评论的信息
                    CommentEntity commentEntity = new CommentEntity();
                    commentEntity.setTargetId(targetId);
                    commentEntity.setContent(content);
                    commentEntity.setCreateTime(createTime);

                    CommentVo vo = new CommentVo();
                    vo.setUserEntity(userEntity);
                    vo.setCommentEntity(commentEntity);

                    commentVoList.add(vo);

                }
                return commentVoList;
            }
        }, id);
    }
}
